package com.angel.activity;

import java.util.HashMap;

import com.angel.entity.Task;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.UserInfoUtil;
import com.umeng.analytics.MobclickAgent;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SinaWeibo extends Activity implements IActivity{

	private Button weibo_back_Btn;
	private Button weibo_binding;
	private Button cancel_weibo_binding;
	private Intent tent;
	private String isBind;
	private Context context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sinaweibo);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		context=SinaWeibo.this;
		tent=getIntent();
		isBind=tent.getStringExtra("isBind");
		MyListener listenr = new MyListener();
		weibo_back_Btn = (Button)findViewById(R.id.weibo_back_bnt);
		cancel_weibo_binding = (Button)findViewById(R.id.cancel_weibo_binding);
		weibo_binding = (Button)findViewById(R.id.weibo_binding);
		weibo_back_Btn.setOnClickListener(listenr);
		if (isBind.equals("已绑定")) {
			weibo_binding.setEnabled(false);
		}else{
			cancel_weibo_binding.setEnabled(false);
		}
	}

	
    class MyListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			int id=v.getId();
			if(id==R.id.weibo_back_bnt){
				finish();
			}else if(id==R.id.outsign_btn){
				
			}
			
		}
    }
    /*解除微博绑定*/
    public void unBindWeibo(View v){
    	UserInfoUtil.clearWeiboInfo(context);
    	finish();//
    }
    /* 绑定微博 */
	public void bindWeibo(View v) {
			UserInfoUtil.oauthRequest(context, new AuthListener());
	}

	class AuthListener implements WeiboDialogListener {
		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			String uid = values.getString("uid");

			AccessToken accessToken = new AccessToken(token,
					Weibo.getAppSecret());
			accessToken.setExpiresIn(expires_in);
			/* 通过Preference用于存储获取到的access_token,access_token_secret */
			SharedPreferences.Editor editor = context.getSharedPreferences(
					UserInfoUtil.USER_TABLE, Context.MODE_WORLD_WRITEABLE)
					.edit();
			/* 获取当前系统时间 */
			long oauthTime = BaseUtil.getTime();
			
			editor.putString("weibo_access_token", token);
			editor.putString("access_token_secret", accessToken.getSecret());
			editor.putString("weibo_expire_in", expires_in);// 存储离过期的时间
			editor.putLong("oauthTime", oauthTime);// 保存当前时间
			editor.putString("uid",uid);//存储用户Id
			editor.commit();// 注意:应该进行保存
			Weibo.getInstance().setAccessToken(accessToken);
			BaseUtil.makeToast(SinaWeibo.this, "您已成功绑定微博");
			Log.i("TAG", "Thread -->name:" + Thread.currentThread().getName()
					+ "   id:" + Thread.currentThread().getId());
			bindWeibo();
			SinaWeibo.this.finish();//关闭当前页面
		}

		@Override
		public void onWeiboException(WeiboException e) {
			BaseUtil.makeToast(SinaWeibo.this,
					"Auth exception : " + e.getMessage());
		}

		@Override
		public void onError(DialogError e) {
			BaseUtil.makeToast(SinaWeibo.this, "认证出现错误 : " + e.getMessage());
		}

		@Override
		public void onCancel() {
			BaseUtil.makeToast(SinaWeibo.this, "认证取消");
		}
	}
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void refresh(Object... params) {
		
	}
	 public void bindWeibo(){
	    	HashMap<String,String> map=new HashMap<String,String>();
			map.put("userId", UserInfoUtil.getValue(context, "userId", ""));
			map.put("weibo",  UserInfoUtil.getValue(context, "uid", ""));
	    	BaseUtil.startService(Task.WEIBOBIND,this,map);//启动登陆
	 }
	 @Override
		protected void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			 MobclickAgent.onResume(this);
		}
		@Override
		protected void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			 MobclickAgent.onPause(this);
		}
}
